<template>
  <a-modal
    v-model:visible="visible"
    :title="formData && formData.id ? '修改广告位' :'新增广告位'"
    draggable
    :width="width"
    @cancel="handleCancel"
  >
    <a-form :model="formData" ref="formRef" auto-label-width>
      <a-spin :loading="loading">
        <a-form-item
          label="广告名称"
          field="name"
          :rules="[{required:true, message:'广告名称不能为空'}]"
          :validate-trigger="['blur']"
        >
          <a-input v-model="formData.name" placeholder="请输入广告名称" allow-clear />
        </a-form-item>
        <a-form-item
          label="跳转路径"
          field="path"
          :rules="[{required:true, message:'跳转路径不能为空'}]"
          :validate-trigger="['blur']"
        >
          <a-input v-model="formData.path" placeholder="请输入跳转路径" allow-clear />
        </a-form-item>
        <a-form-item
          label="图片"
          field="path"
          :rules="[{required:true, message:'图片不能为空'}]"
          :validate-trigger="['blur']"
        >
          <UploadImage v-model="formData.logo" :limit="1" ></UploadImage>
        </a-form-item>
        <a-form-item
          label="排序"
          field="sort"
          :rules="[{required:true, message:'排序不能为空'}]"
          :validate-trigger="['blur']"
        >
          <a-input-number v-model="formData.sort" placeholder="请输入排序" allow-clear />
        </a-form-item>
        <a-form-item
          label="广告状态"
          field="status"
          :rules="[{required:true, message:'广告状态不能为空'}]"
          :validate-trigger="['blur']"
        >
          <a-switch type="round" v-model="formData.status" :checked-value="1" :unchecked-value="0">
            <template #checked>{{ $t('searchTable.operation.enabled') }}</template>
            <template #unchecked>{{ $t('searchTable.operation.disabled') }}</template>
          </a-switch>
        </a-form-item>
      </a-spin>
    </a-form>
    <template #footer>
      <a-button
        style="margin-right: 8px"
        @click="handleCancel"
      >{{ $t('searchTable.columns.operations.close') }}</a-button>
      <a-button
        type="primary"
        @click="handleSubmit"
        :loading="loading"
      >{{ $t('searchTable.columns.operations.save') }}</a-button>
    </template>
  </a-modal>
</template>
<script setup lang="ts">
import { ref, watch, nextTick } from 'vue';
import { useI18n } from 'vue-i18n';
import { Modal, Message } from '@arco-design/web-vue';
import { saveAdvertising, updateAdvertising, AdvertisingData } from '@/api/app/advertising';

import UploadImage from '@/components/upload-image/index.vue';

const { t } = useI18n();

const loading = ref(false);
const visible = ref(false);
const width = ref('calc(28%)');

const formRef = ref();
const formData = ref<AdvertisingData>({
  status: 0,
});
const changed = ref(false);

const emit = defineEmits({ onSearch: null });

// 打开弹窗
const onOpen = (record: any) => {
  if (record) {
    formData.value = JSON.parse(JSON.stringify(record));
  } else {
    formData.value = {
      status: 0,
    };
  }
  nextTick(() => {
    changed.value = false;
  });
  visible.value = true;
};

// 确认
const handleSubmit = async () => {
  formRef.value.validate().then((result: any) => {
    // 校验不通过
    if (result) {
      return;
    }
    loading.value = true;
    if (formData.value.id) {
      updateAdvertising(formData.value)
        .then(() => {
          onOk();
        })
        .finally(() => {
          loading.value = false;
        });
    } else {
      saveAdvertising(formData.value)
        .then(() => {
          onOk();
        })
        .finally(() => {
          loading.value = false;
        });
    }
  });
};

// 确认
const onOk = () => {
  formRef.value.clearValidate();
  formData.value = {};
  visible.value = false;
  Message.success(t('operations.success'));
  emit('onSearch');
};

// 取消
const handleCancel = () => {
  if (!changed.value) {
    onCancel();
  } else {
    Modal.confirm({
      title: t('operations.cancel.title'),
      content: '',
      onOk() {
        onCancel();
      },
      onCancel() {
        visible.value = true;
      },
    });
  }
};

// 取消
const onCancel = () => {
  formRef.value.clearValidate();
  formData.value = {};
  visible.value = false;
};

// 抛出函数
defineExpose({
  onOpen,
});

// 监听数据
watch(
  formData,
  (newValue, oldValue) => {
    changed.value = true;
  },
  { deep: true }
);
</script>